package util.data.carddao;

import java.sql.SQLException;
import java.util.List;

import business.entity.Card;
import business.entity.CardDBAdapter;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class CardDataContext implements ICardDataContext {
	/**
	 * @param context 当前的上下文对象
	 */
	public CardDataContext(Context context){
		this.context = context;
		//初始化createtableSQL
		String cardSQL = CardDBAdapter.getCreateTableSQL();
		this.createTable = new String[]{cardSQL};
		//初始化DataContext
		this.helper = new CardDBHelper(context, dbName, createTable, null,
				version);
//		helper.getWritableDatabase() = helper.getWritableDatabase();
//		getReadableDatabase = helper.getReadableDatabase();
	}
	
	public CardDataContext(Context context, String [] createTable){
		this.context = context;
		//初始化DataContext
		this.helper = new CardDBHelper(context, dbName, createTable, null,
				version);
	}
	
	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public String getDbName() {
		return dbName;
	}

	public void setDbName(String dbName) {
		this.dbName = dbName;
	}

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	private int version = 1;
	private String[] createTable;
	private String dbName = "MyCard.db";
	private Context context = null;
	private CardDBHelper helper = null;
	
	@Override
	public int add(Card card) throws SQLException {
		System.out.println("增加开始");
		SQLiteDatabase db = helper.getWritableDatabase();
 
	    db.execSQL(CardDBAdapter.getInsertSQL(), 
				   CardDBAdapter.getInsertInstance(card));
	    
	    //获得最后插入的 Id
	    Cursor cursor = db.rawQuery("select last_insert_rowid() from card",null); 
	    
	    int id = -1;    
	    if(cursor.moveToFirst())  
	       id = cursor.getInt(0);  
		cursor.close();
		db.close();
		
		System.out.println("增加结束");
		return id;
	}

	@Override
	public void delete(int ID) throws SQLException {
		System.out.println("数据库删除开始");
		SQLiteDatabase db = helper.getWritableDatabase();
		String[] whereArgs = new String []{String.valueOf(ID)};
		db.delete("card", "cardId=?", whereArgs);
		System.out.println("数据库删除结束");
		db.close();
	}

	@Override
	public void update(Card card) throws SQLException {
		System.out.println("数据库修改开始");
		int ID = card.getId();
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues values = CardDBAdapter.getContentValues(card);
		String[] whereArgs = new String []{String.valueOf(ID)};
		db.update("card", values, "cardId=?", whereArgs);
		System.out.println("数据库修改结束");
		db.close();
	}


	/* 
	 * 根据Id查找Card如果没有发现会返回null
	 * 
	 */
	@Override
	public Card queryById(int ID) throws SQLException {
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery(CardDBAdapter.getQueryByIdSQL(), 
				new String[]{String.valueOf(ID)});
		List<Card> list = CardDBAdapter.convertFromCursor(cursor);
		cursor.close();
		db.close();
		if(list.size() == 1)
			return list.get(0);
		return null;
	}

	@Override
	public List<Card> queryForAll() throws SQLException {
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery(CardDBAdapter.getQueryAllSQL(),null);
		List<Card> list = CardDBAdapter.convertFromCursor(cursor);
		cursor.close();
		db.close();
		return list;
	}

	@Override
	public List<Card> queryBySql(String sql, String[] args) throws SQLException {
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor cursor = db.rawQuery(sql, args);
		List<Card> list = CardDBAdapter.convertFromCursor(cursor);
		cursor.close();
		db.close();
		return list;
	}

	@Override
	public List<Card> queryByName(String name) throws SQLException {
		System.out.println("开始查询");
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery(CardDBAdapter.getQueryByNameSQL(),
				               new String[]{name});
		List<Card> list = CardDBAdapter.convertFromCursor(cursor);
		cursor.close();
		db.close();
		System.out.println("结束查询");
		return list;
	}
}
